<template>
  <el-card class="content-card" shadow="hover">
    <template #header>
      <div class="card-header">
        <span>API文档 - 快速开始</span>
      </div>
    </template>

    <div class="content-container">
      <div class="api-content">
        <div class="section" id="prerequisites">
          <div class="section-header"><h2>前置条件</h2></div>
          <p class="intro-text">
            在开始使用我们的API之前，请确保您已经满足以下条件：
          </p>
          <ul class="requirements-list">
            <li>拥有有效的API密钥（请联系管理员获取）</li>
            <li>熟悉HTTP请求和JSON数据格式</li>
            <li>具备基本的编程知识</li>
            <li>开发环境支持HTTPS请求</li>
          </ul>
        </div>

        <div class="section" id="authentication">
          <div class="section-header">
            <h2>身份认证</h2>
            <el-button
              type="primary"
              size="small"
              :icon="CopyDocument"
              @click="copyCode(authExample)"
              class="copy-btn"
            >
              复制代码
            </el-button>
          </div>
          <p>所有API请求都需要在请求头中包含您的API密钥：</p>
          <MonacoEditor
            style="height: 120px; border-radius: 6px; overflow: hidden"
            v-model="authExample"
            language="javascript"
            :options="{
              readOnly: true,
              minimap: { enabled: false },
              height: '120px',
            }"
          />
        </div>

        <div class="section" id="first-request">
          <div class="section-header">
            <h2>第一个请求</h2>
            <el-button
              type="primary"
              size="small"
              :icon="CopyDocument"
              @click="copyCode(firstRequestExample)"
              class="copy-btn"
            >
              复制代码
            </el-button>
          </div>
          <p>让我们从一个简单的GET请求开始：</p>
          <MonacoEditor
            style="height: 200px; border-radius: 6px; overflow: hidden"
            v-model="firstRequestExample"
            language="javascript"
            :options="{
              readOnly: true,
              minimap: { enabled: false },
              height: '200px',
            }"
          />
        </div>

        <div class="section" id="response-format">
          <div class="section-header">
            <h2>响应格式</h2>
            <el-button
              type="primary"
              size="small"
              :icon="CopyDocument"
              @click="copyCode(responseExample)"
              class="copy-btn"
            >
              复制代码
            </el-button>
          </div>
          <p>所有API响应都遵循统一的JSON格式：</p>
          <MonacoEditor
            style="height: 180px; border-radius: 6px; overflow: hidden"
            v-model="responseExample"
            language="json"
            :options="{
              readOnly: true,
              minimap: { enabled: false },
              height: '180px',
            }"
          />
        </div>

        <div class="section" id="error-handling">
          <div class="section-header"><h2>错误处理</h2></div>
          <p>当请求出现错误时，API会返回相应的HTTP状态码和错误信息：</p>
          <div class="error-codes">
            <div class="error-item">
              <span class="error-code">400</span>
              <span class="error-desc">请求参数错误</span>
            </div>
            <div class="error-item">
              <span class="error-code">401</span>
              <span class="error-desc">身份认证失败</span>
            </div>
            <div class="error-item">
              <span class="error-code">403</span>
              <span class="error-desc">权限不足</span>
            </div>
            <div class="error-item">
              <span class="error-code">404</span>
              <span class="error-desc">资源不存在</span>
            </div>
            <div class="error-item">
              <span class="error-code">500</span>
              <span class="error-desc">服务器内部错误</span>
            </div>
          </div>
        </div>

        <div class="section" id="next-steps">
          <div class="section-header"><h2>下一步</h2></div>
          <p>现在您已经了解了基本的使用方法，建议您：</p>
          <div class="next-steps-grid">
            <div class="step-card">
              <div class="step-number">1</div>
              <h3>查看基础配置</h3>
              <p>了解更多配置选项和环境设置</p>
            </div>
            <div class="step-card">
              <div class="step-number">2</div>
              <h3>浏览API方法</h3>
              <p>探索所有可用的API接口和功能</p>
            </div>
            <div class="step-card">
              <div class="step-number">3</div>
              <h3>查看使用案例</h3>
              <p>学习实际的应用场景和最佳实践</p>
            </div>
          </div>
        </div>
      </div>

      <!-- 右侧悬浮目录 -->
      <div class="toc">
        <div class="toc-title">目录</div>
        <div class="toc-item" @click="scrollToSection('prerequisites')">
          前置条件
        </div>
        <div class="toc-item" @click="scrollToSection('authentication')">
          身份认证
        </div>
        <div class="toc-item" @click="scrollToSection('first-request')">
          第一个请求
        </div>
        <div class="toc-item" @click="scrollToSection('response-format')">
          响应格式
        </div>
        <div class="toc-item" @click="scrollToSection('error-handling')">
          错误处理
        </div>
        <div class="toc-item" @click="scrollToSection('next-steps')">
          下一步
        </div>
      </div>
    </div>
  </el-card>
</template>

<script setup>
import { ref } from "vue";
import { ElMessage } from "element-plus";
import { CopyDocument } from "@element-plus/icons-vue";
import MonacoEditor from "@/components/MonacoEditor.vue";

const authExample = ref(`// 在请求头中添加API密钥
const headers = {
  'Authorization': 'Bearer YOUR_API_KEY',
  'Content-Type': 'application/json'
};`);

const firstRequestExample = ref(`// 使用fetch发送GET请求
fetch('https://api.example.com/v1/data', {
  method: 'GET',
  headers: {
    'Authorization': 'Bearer YOUR_API_KEY',
    'Content-Type': 'application/json'
  }
})
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error('Error:', error));`);

const responseExample = ref(`{
  "code": 200,
  "message": "success",
  "data": {
    // 实际数据内容
  },
  "timestamp": "2024-01-01T00:00:00Z"
}`);

const scrollToSection = (sectionId) => {
  const element = document.getElementById(sectionId);
  if (element) {
    element.scrollIntoView({ behavior: "smooth" });
  }
};

const copyCode = async (code) => {
  if (!code) {
    ElMessage.warning("没有可复制的代码");
    return;
  }

  try {
    await navigator.clipboard.writeText(code);
    ElMessage.success("代码已复制到剪贴板");
  } catch (err) {
    // 降级方案：使用传统的复制方法
    const textArea = document.createElement("textarea");
    textArea.value = code;
    document.body.appendChild(textArea);
    textArea.select();
    try {
      document.execCommand("copy");
      ElMessage.success("代码已复制到剪贴板");
    } catch (fallbackErr) {
      ElMessage.error("复制失败，请手动复制");
    }
    document.body.removeChild(textArea);
  }
};
</script>

<style scoped>
.content-card {
  height: 100%;
  display: flex;
  flex-direction: column;
  position: relative;
  border-radius: 12px;
  overflow: hidden;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
}

.card-header {
  box-sizing: border-box;
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 18px;
  font-weight: 500;
  color: #303133;
}

:deep(.el-card__body) {
  padding: 0;
  height: calc(100% - 60px);
  overflow-y: auto;
  background: #ffffff;
}

.content-container {
  min-height: 100%;
  padding: 30px;
  padding-right: 280px;
  background: linear-gradient(to bottom, #ffffff 0%, #f8f9fa 100%);
}

.api-content {
  max-width: 1000px;
  margin: 0 auto;
  line-height: 1.8;
  color: #4a5568;
}

.section {
  margin-bottom: 40px;
  padding: 0;
}

.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.section h2 {
  margin: 0;
  padding-bottom: 10px;
  border-bottom: 2px solid #409eff;
  color: #303133;
  font-size: 20px;
  font-weight: 600;
  position: relative;
  flex: 1;
}

.section h2::before {
  content: "";
  position: absolute;
  left: 0;
  bottom: -2px;
  width: 50px;
  height: 2px;
  background: #409eff;
  border-radius: 1px;
}

.copy-btn {
  margin-left: 16px;
  border-radius: 6px;
  font-size: 12px;
  padding: 6px 12px;
  background: #409eff;
  border-color: #409eff;
  transition: all 0.3s ease;
}

.copy-btn:hover {
  background: #337ecc;
  border-color: #337ecc;
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(64, 158, 255, 0.3);
}

.intro-text {
  font-size: 16px;
  line-height: 1.8;
  color: #4a5568;
  margin-bottom: 20px;
}

.requirements-list {
  list-style: none;
  padding: 0;
}

.requirements-list li {
  padding: 12px 0;
  border-bottom: 1px solid #e2e8f0;
  font-size: 15px;
  line-height: 1.6;
  position: relative;
  padding-left: 24px;
}

.requirements-list li:before {
  content: "✓";
  position: absolute;
  left: 0;
  color: #409eff;
  font-weight: bold;
}

.requirements-list li:last-child {
  border-bottom: none;
}

.error-codes {
  display: grid;
  gap: 12px;
  margin-top: 20px;
}

.error-item {
  display: flex;
  align-items: center;
  padding: 12px 16px;
  background: #f8f9fa;
  border-radius: 8px;
  border-left: 4px solid #e74c3c;
}

.error-code {
  font-weight: bold;
  color: #e74c3c;
  margin-right: 16px;
  min-width: 40px;
}

.error-desc {
  color: #4a5568;
}

.next-steps-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  gap: 20px;
  margin-top: 20px;
}

.step-card {
  background: #ffffff;
  padding: 24px;
  border-radius: 12px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
  border: 1px solid #e2e8f0;
  transition: all 0.3s ease;
  text-align: center;
}

.step-card:hover {
  transform: translateY(-4px);
  box-shadow: 0 8px 25px rgba(0, 0, 0, 0.15);
}

.step-number {
  width: 40px;
  height: 40px;
  background: #409eff;
  color: white;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: bold;
  font-size: 18px;
  margin: 0 auto 16px;
}

.step-card h3 {
  margin: 0 0 12px 0;
  color: #2d3748;
  font-size: 18px;
  font-weight: 600;
}

.step-card p {
  margin: 0;
  color: #4a5568;
  font-size: 14px;
  line-height: 1.6;
}

/* 右侧悬浮目录样式 */
.toc {
  position: fixed;
  right: 50px;
  top: 50%;
  transform: translateY(-50%);
  background: #ffffff;
  padding: 20px;
  border-radius: 8px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
  z-index: 1000;
  border: 1px solid #e4e7ed;
  min-width: 200px;
}

.toc-title {
  font-weight: 700;
  margin-bottom: 15px;
  color: #2d3748;
  font-size: 18px;
  text-align: center;
  padding-bottom: 10px;
  border-bottom: 2px solid #e2e8f0;
}

.toc-item {
  padding: 12px 16px;
  cursor: pointer;
  color: #4a5568;
  transition: all 0.3s ease;
  border-radius: 8px;
  font-size: 14px;
  margin-bottom: 4px;
  border-left: 3px solid transparent;
  position: relative;
}

.toc-item:hover {
  color: #409eff;
  background: #ecf5ff;
  border-left-color: #409eff;
  transform: translateX(4px);
}

.toc-item:active {
  transform: translateX(2px);
}

/* Monaco Editor 样式优化 */
:deep(.monaco-editor) {
  border-radius: 8px;
  border: 1px solid #e2e8f0;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
}

/* 响应式设计 */
@media (max-width: 1400px) {
  .content-container {
    padding-right: 30px;
  }

  .toc {
    display: none;
  }
}

@media (max-width: 768px) {
  .content-container {
    padding: 20px 15px;
  }

  .section {
    padding: 20px 15px;
    margin-bottom: 25px;
  }

  .section h2 {
    font-size: 18px;
  }

  .card-header {
    font-size: 16px;
    padding: 15px;
  }

  .next-steps-grid {
    grid-template-columns: 1fr;
  }
}

/* 滚动条样式 */
:deep(.el-card__body)::-webkit-scrollbar {
  width: 6px;
}

:deep(.el-card__body)::-webkit-scrollbar-track {
  background: #f1f1f1;
  border-radius: 3px;
}

:deep(.el-card__body)::-webkit-scrollbar-thumb {
  background: #409eff;
  border-radius: 3px;
}

:deep(.el-card__body)::-webkit-scrollbar-thumb:hover {
  background: #337ecc;
}
</style>